package com.hejjon.core.bean;

import com.hejjon.core.enumer.DataViewTypeEnum;
import com.hejjon.core.enumer.JdbcTypeEnum;
import lombok.Data;

@Data
public class TableField {

    private String name;

    private String alias;

    private String label;

    private String jdbcType;

    private String javaType;

    private Object value;

    private String tableName;

    private Boolean isSort;

    private String direction;

    private Boolean isShow;

    // 是否是日期类型 1是0否
    private Boolean isDate;

    private Boolean isDatetime;

    // 是否是数值类型 包括 int, long, longint, double 等
    private Boolean isNumber;

    // 是否是用户自定义字段, 区分默认字段
    private Boolean isCustom;

    public Boolean getIsDate() {
        return JdbcTypeEnum.DATE.getCode().equalsIgnoreCase(this.getJdbcType());
    }

    public Boolean getIsDatetime() {
        return JdbcTypeEnum.DATETIME.getCode().equalsIgnoreCase(this.getJdbcType());
    }

    public Boolean getIsNumber() {
        return JdbcTypeEnum.INT.getCode().equalsIgnoreCase(this.getJdbcType())
                || JdbcTypeEnum.BIGINT.getCode().equalsIgnoreCase(this.getJdbcType())
                || JdbcTypeEnum.DOUBLE.getCode().equalsIgnoreCase(this.getJdbcType());
    }

    /**
     * 默认列转成默认字段
     */
    public static TableField ofDefault(TableColumn column) {
        TableField tf = new TableField();
        tf.setName(column.getColumnName());
        tf.setLabel(column.getComment());
        tf.setJdbcType(column.getJdbcType());
        tf.setJavaType(JdbcTypeEnum.getJavaTypeBy(JdbcTypeEnum.getInstance(column.getJdbcType())));
        tf.setIsNumber(DataViewTypeEnum.getIsNumber(column.getJdbcType()));
        tf.setIsDate(DataViewTypeEnum.getIsDate(column.getJdbcType()));
        tf.setValue(column.getDefaultValue());
        tf.setIsCustom(false);   // 默认字段都不是自定义字段
        return tf;
    }

    public TableField() {
    }

    private TableField(String name) {
        this.name = name;
    }

    public static TableField getInstance(String name) {
        return new TableField(name);
    }



}
